package joapp

import (
	"fmt"
)

type AppFlagMgr struct {
	registeredCmd     map[string]*AppCmd
	registeredFlagLst []IAppFlag
	cmdTarget         []string
	runCmd            string
}

func NewAppFlagMgr() *AppFlagMgr {
	return &AppFlagMgr{
		registeredCmd:     make(map[string]*AppCmd),
		registeredFlagLst: make([]IAppFlag, 0),
	}
}

func (mgr *AppFlagMgr) RegisterMainCmd(handler AppCmdHandler) {
	if _, ok := mgr.registeredCmd[""]; ok {
		panic("main cmd has registered already")
	}

	mgr.registeredCmd[""] = NewAppCmd(handler, "", "")
}

func (mgr *AppFlagMgr) RegisterCmd(cmd *AppCmd) {
	if cmd == nil {
		panic("can not register nil command")
	}

	cmdName := cmd.GetName()
	handler := cmd.GetHandler()

	if cmdName == "" {
		panic("can not register command with empty name")
	}

	if _, ok := mgr.registeredCmd[cmdName]; ok {
		panic(fmt.Sprintf("cmd '%s' has registered already", cmdName))
	}

	if handler == nil {
		panic("can not register command with nil handler")
	}

	mgr.registeredCmd[cmdName] = cmd
}
